home *** CD-ROM | disk | FTP | other *** search
/ The 640 MEG Shareware Studio 2 / The 640 Meg Shareware Studio CD-ROM Volume II (Data Express)(1993).ISO / clang / cuj1008.zip / 1008086A < prev    next >
Text File  |  1991-12-04  |  4KB  |  183 lines

  1. // main2.cpp
  2. // A demonstration of a database buffer
  3. // made simpler by smart pointers.
  4.  
  5. #define DEBUG
  6.  
  7. #include "anyclass.hpp"
  8.  
  9. class BufferedDatabase
  10.  {
  11.   public:
  12.     BufferedDatabase (void){};
  13.     ~BufferedDatabase (void);
  14.  
  15.     void getRecPtr (
  16.         int recNum,
  17.         RefCntPtr(anyClass)& ptr);
  18.  
  19.   private:
  20.     enum {bfrSize=3};
  21.  
  22.     RefCntPtr(anyClass) bfr[bfrSize];
  23.  
  24.     // these functions would actually
  25.     // read from the disk DB.
  26.     // for now, we'll just pretend
  27.     void getRecWithNew (
  28.         int recNum,
  29.         RefCntPtr(anyClass)& ptr);
  30.  
  31.     void putRec (
  32.         RefCntPtr(anyClass)& ptr);
  33.  };
  34.  
  35.  
  36. void BufferedDatabase::getRecPtr (
  37.         int recNum,
  38.         RefCntPtr(anyClass)& ptr)
  39.   {
  40.     // first, see if it is in the buffer
  41.  
  42.     for (int i = 0; i < bfrSize; ++i)
  43.         if (  bfr[i]
  44.            && (bfr[i]->intVal == recNum)
  45.            )
  46.           {
  47.             ptr = bfr[i];
  48.             return;
  49.           };
  50.  
  51.     // if we made it to here, then it
  52.     // is not in the buffer, so get it
  53.     // from disk;
  54.  
  55.     RefCntPtr(anyClass) tmp;
  56.     getRecWithNew (recNum, tmp);
  57.  
  58.     // return if not found
  59.     if (0 == tmp)
  60.       {
  61.         ptr = 0;
  62.         return;
  63.       };
  64.  
  65.     // see if it can be inserted
  66.     // into buffer
  67.     for (i = 0; i < bfrSize; ++i)
  68.         if (0 == bfr[i])
  69.           {
  70.             bfr[i] = tmp;
  71.             ptr = tmp;
  72.             return;
  73.           };
  74.  
  75.     ptr = 0;
  76.  
  77.     // if it didn't return, then there
  78.     // is no space - will have to find
  79.     // some.
  80.     for (i = 0; i < bfrSize; ++i)
  81.  
  82.       // reference count of 1 means the
  83.       // only pointer to this object
  84.       // is bfr[i].  Since nothing
  85.       // outside this routine is pointing
  86.       // to it, we can get rid of it.
  87.  
  88.         if (bfr[i]->refCnt() == 1)
  89.           {
  90.             putRec (bfr[i]);
  91.             bfr[i] = tmp;
  92.             ptr = tmp;
  93.             return;
  94.           };
  95.  
  96.     // if still didn't return,
  97.     // out of space
  98.  
  99.     cout << "BUFFER FULL\n\n";
  100.     // obviously, running out of space
  101.     // is a bad thing - a real-world
  102.     // scheme wouldn't use as array,
  103.     // but some form of dynamic
  104.     // allocation
  105.  
  106.   };
  107.  
  108.  
  109. void BufferedDatabase::getRecWithNew (
  110.         int recNum,
  111.         RefCntPtr(anyClass)& ptr)
  112.   {
  113.     // would get record from database
  114.     // again, we'll just fake it
  115.  
  116.     // pretend that records 1000
  117.     // and above don't exist
  118.  
  119.     if (recNum >= 1000)
  120.       {
  121.         ptr = 0;
  122.         return;
  123.       };
  124.  
  125.     ptr = new anyClass;
  126.  
  127.     // pretend that it's
  128.     // getting a record from disk
  129.     ptr->intVal = recNum;
  130.     ptr->string = "something";
  131.  
  132.     cout << "getting from disk - rec # "
  133.          << ptr->intVal << "\n\n";
  134.   };
  135.  
  136.  
  137. void BufferedDatabase::putRec (
  138.         RefCntPtr(anyClass)& ptr)
  139.   {
  140.     cout << "putting to disk - rec # "
  141.          << ptr->intVal << "\n\n";
  142.   };
  143.  
  144.  
  145. BufferedDatabase::~BufferedDatabase(void)
  146.   {
  147.     for (int i = 0; i < bfrSize; ++i)
  148.         if (bfr[i])
  149.           {
  150.             putRec (bfr[i]);
  151.             bfr[i] = 0;
  152.           };
  153.   };
  154.  
  155.  
  156. void main (void)
  157.   {
  158.    cout << "start of main\n\n";
  159.    BufferedDatabase db;
  160.  
  161.    RefCntPtr(anyClass) ptrA, ptrB,
  162.                        ptrC, ptrD;
  163.  
  164.    db.getRecPtr (1, ptrA);
  165.    ptrA->show();
  166.  
  167.    db.getRecPtr (2, ptrA);
  168.    db.getRecPtr (3, ptrA);
  169.    db.getRecPtr (4, ptrA);
  170.  
  171.    db.getRecPtr (2, ptrA);
  172.    db.getRecPtr (3, ptrB);
  173.    db.getRecPtr (4, ptrC);
  174.    db.getRecPtr (5, ptrD);
  175.    if (!ptrD)
  176.      cout << "getRecFailed\n\n";
  177.  
  178.    db.getRecPtr (1005, ptrD);
  179.    ptrD->show();//an error
  180.  
  181.    cout << "end of main\n\n";
  182.   };
  183.